from django.shortcuts import render
from django.shortcuts import HttpResponse
from App_demo_2 import models

# Create your views here.
# 业务处理逻辑都在views.py文件里。

# https://blog.csdn.net/gavinking0110/article/details/54412590

'''
model一般都是有多个属性的，但是很多时候我们又只需要查询特定的某一个，这个时候可以用到values和values_list
values和values_list的差别:
    vlaues -
        单条记录 - <class 'dict'>
        多条记录 - <class 'django.db.models.query.QuerySet'>
    vlaues_list -
        单条记录 - <class 'tuple'>
        多条记录 - <class 'django.db.models.query.QuerySet'>
'''

# def index(request): # request入参必须有
#     # 不能直接返回字符串
#     return HttpResponse("Hello world ! ")


# # 新增index.html文件后需要修改
# def index(request): # request入参必须有
#     return render(request, "index.html")


# # 新增动态交互
# def index(request): # request入参必须有
#     if request.method == "POST":
#         user_name = request.POST.get("user_name", None)
#         password = request.POST.get("password", None)
#         print(user_id, user_name, password)
#
#     return render(request, "index.html")



# # 返回动态页面
# # 需要新增用户信息列表user_list
# user_list = [
#     {"user_name": "iack", "password": "abc"},
#     {"user_name": "tom", "password": "ABC"}
# ]
#
# def index(request): # request入参必须有
#     if request.method == "POST":
#         user_name = request.POST.get("user_name", None)
#         password = request.POST.get("password", None)
#
#         temp = {"user_name":user_name, "password":password}
#         user_list.append(temp)
#
#     return render(request, "index.html", {"data":user_list})


# 使用sqlite3数据库
def index(request): # request入参必须有
    context = {}
    context['home'] = 'Hello World!'
    return render(request, "index.html", context)


# 注册
def register(request):
    # Model获取指定列的数据:获取一个字段
    dict_user_id = models.UserInfo.objects.values("user_id").last()
    if dict_user_id:
        user_id = int(dict_user_id["user_id"]) + 1
    else:
        user_id = 1

    if request.method == "POST":
        user_name = request.POST.get("user_name", None)
        telephone = request.POST.get("telephone", None)
        password = request.POST.get("password", None)

        # 添加数据到sqlite3数据库
        models.UserInfo.objects.create(user_id=user_id,
                                       user_name=user_name,
                                       telephone=telephone,
                                       password=password)

    # 从数据库读取所有数据
    table_userinfo = models.UserInfo.objects.all()

    return render(request, "register.html", {"data":table_userinfo})


# 登录
def login(request):
    flag = False
    if request.method == "POST":
        telephone = request.POST.get("telephone", None)
        password = request.POST.get("password", None)

        # Model获取指定列的数据:返回指定的字段
        db_password = models.UserInfo.objects.using('default').get(telephone=telephone).password
        # dict_password = models.UserInfo.objects.using('default').values(password).filter(telephone=telephone)

        if password == db_password:
            flag = True

    context = {}
    if flag:
        context['flag'] = '登录成功'
    else:
        context['flag'] = '登录失败'

    return render(request, "login.html", context)
